package com.hjm.java.service;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.hjm.java.entity.HomeBanner;
import com.hjm.java.entity.Goods;
import com.hjm.java.sql.HJMHelper;
import com.hjm.java.utils.HjmGlideUtil;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.java.utils.HjmSuccessCallBack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商品服务
 */
public class GoodsService extends BaseService {
    private final String TAG = "GoodsService";
    private List<HomeBanner> bannerList;

    public GoodsService(Context mContext, Handler mHandler) {
        super(mContext, mHandler);
    }

    public void getGoodsDetail(int goodsId, HjmObjectCallBack<Goods> callBack) {
        httpUtils.get(baseUrl + "/goods/" + goodsId, Goods.class, callBack);
    }

    /**
     * 获取轮播图
     * @param bannerIds         轮播图id数组
     * @param successCallBack   成功回调
     */
    public void getBanner(int[] bannerIds, HjmSuccessCallBack successCallBack) {
        bannerList = new ArrayList<>();
        String[] bannerIdsArr = Arrays.stream(bannerIds).mapToObj(String::valueOf).toArray(String[]::new);
        HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl, bannerIdsArr,
                HJMHelper.getInstance(mContext),
                (position, bitmap) -> {
                    HomeBanner homeBanner = new HomeBanner();
                    homeBanner.setImg(bitmap);
                    bannerList.add(homeBanner);
                },
                () -> {
                    mHandler.post(()->{
                        successCallBack.onSuccess();
                    });
                });
    }

    public List<HomeBanner> getBannerList() {
        return bannerList;
    }

    public void setGoodsContent(int[] imageIds,HjmObjectCallBack<String> callBack){

//        String[] imageIdsArr = Arrays.stream(imageIds).mapToObj(String::valueOf).toArray(String[]::new);
//        HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl, imageIdsArr,HJMHelper.getInstance(mContext),
//                (position, bitmap) -> {
//                    // 需要将bitmap转化为base64
//                    String base64 = HjmGlideUtil.bitmapToBase64(bitmap);
//                    String javascriptCode = "loadBitmap('" + base64 + "');";
////                    Log.d(TAG, javascriptCode);
////                    String javascriptCode = "loadBitmap('123');";
//                    mHandler.post(() -> {
//                        callBack.call(true, javascriptCode, null);
//                    });
//                },
//                () -> {});
        mHandler.post(() -> {
            for (int i = 0; i < imageIds.length; i++) {
                String javascriptCode = "loadImage('" + baseImgUrl + imageIds[i] + "');";
                callBack.call(true, javascriptCode,null);
            }
        });
    }
}
